import json
import datetime

import requests
from django.conf import settings


def get_zhsm_token():
    url = settings.ZHSM_LOGIN_URL
    user_name = settings.ZHSM_USERNAME
    pass_word = settings.ZHSM_PASSWORD
    headers = {
        'appid': '5',
        'transno': '083cdfde-6845-3191-3764-b7d9560d06c5',
        'content-type': 'application/json;charset=UTF-8',
        'timestamp': '1598860192',
        'sign': 'e696484649d2e140f0b2363eb3ad0382'
    }
    js = {
        "UserLoginName": user_name,
        "PassWord": pass_word,
        "LoginClientType": '1',
        "NeedCheckConflict": '1',
        "LoginClientIP": "115.196.191.102",
        "LoginClientIPAddress": "浙江省杭州市"
    }
    res = requests.post(url=url, headers=headers, json=js)
    data = json.loads(res.text).get('Data')
    if data:
        token = data.get('Token')
    else:
        token = None
    return token


def get_kingdee_token():
    url = settings.KINGDEE_TOKEN_URL
    res = requests.get(
        url=url,
        params={
            'username': settings.KINGDEE_USERNAME,
            'password': settings.KINGDEE_PASSWORD,
            'client_id': settings.KINGDEE_CLIENT_ID,
            'client_secret': settings.KINGDEE_CLIENT_SECRET,
        }
    )
    data = json.loads(res.text).get('data')
    if data:
        token = data.get('access_token')
    else:
        token = None
    return token


